package zy.blogs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zy.blogs.entity.SysUser;
import zy.blogs.service.SysUserService;

import java.util.List;

@Controller
@RequestMapping("/user")
public class PricingStandController {
    SysUserService service;
    @Autowired
    public PricingStandController(SysUserService service){
        this.service=service;
    }
    @RequestMapping("/list")
    public String list(Model model){
        List<SysUser> list=service.getAll();
        model.addAttribute("list",list);
        return "/user/list";
    }
    @RequestMapping(value = "/inser")
    public String insert(Model model){
        model.addAttribute("user",new SysUser());
        return "/user/edit";
    }
    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable("id")Long id, Model model){
        SysUser entity=service.getById(id);
        model.addAttribute("user",entity);
        return "/user/edit";
    }
    @RequestMapping(value = "/save",method= RequestMethod.POST)
    public String save(@ModelAttribute("user")SysUser entity){
        boolean result=entity.getId()!=null?service.update(entity):service.insert(entity);
        return "redirect/user/list()";
    }
    @RequestMapping(value = "/detail/{id}")
    public String detail(@PathVariable("id")Long id,Model model){
        SysUser entity=service.getById(id);
        model.addAttribute("user",entity);
        return "/user/detail";
    }
    @RequestMapping(value = "/detail/{id}")
    public String detail(@PathVariable("id")Long id){
        boolean result=service.delete(id);
        return "redirect/user/detail";
    }
}